//==================================================================
/// sprite.cpp
///
/// Created by Davide Pasca - 2009/10/5
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#include "stdafx.h"
#include "sprite.h"

//==================================================================
sprite::sprite( const DStr &fname, int align_x, int align_y, float base_scale )
{
	mSx = 0;
	mSy = 0;
	mX	= 0;
	mY	= 0;
	mZ	= 0;
	mDx	= 0;
	mDy	= 0;

	mAlignX = align_x;
	mAlignY = align_y;
	mBaseScale = base_scale;
	mpApp = 0;
	mHasShadow = true;
	mFramesN	= 0;

	add_frame( fname );
}

//==================================================================
sprite::sprite( const image *pImg, int align_x, int align_y, float base_scale )
{
	mSx = 0;
	mSy = 0;
	mX	= 0;
	mY	= 0;
	mZ	= 0;
	mDx	= 0;
	mDy	= 0;

	mAlignX = align_x;
	mAlignY = align_y;
	mBaseScale = base_scale;
	mpApp = 0;
	mHasShadow = true;
	mFramesN	= 0;

	add_frame( new image( *pImg ) );
}

//==================================================================
void sprite::create_flipped_x()
{
	for (int i=0; i < mFramesN; ++i)
	{
		const image &src = *mpoImages[i];
			  image *pDes = new image( src );

		mpoImagesFlx.push_back( pDes );

		for (U32 y=0; y < src.mH; ++y)
		{
			U32 yi = y * src.mW;
			for (U32 x=0; x < src.mW; ++x)
				pDes->SetPixelPtr( (int)src.mW-1 - x, y, src.GetPixelPtr( x, y ) );
		}
	}
}

void sprite::shadow_off()
{
	mHasShadow = false;
}

int sprite::add_frame( image *pImg )
{
	mpoImages.push_back( pImg );
	++mFramesN;

	return 0;
}

int sprite::add_frame( const DStr &fname )
{
	add_frame( new image( fname ) );

	return 0;
}

int sprite::width_get( int frame ) const
{
	return mpoImages[frame]->mW;
}

void sprite::draw( graphics &g, int frame, int x, int y, float scale )
{
	draw( g, frame, x, y, scale, 0 );
}

void sprite::draw( graphics &g, int frame, int x1a, int x2a, int ya, int x1b, int x2b, int yb, int flags )
{
	image	*pImg;

	if ( frame < 0 )
		frame = -frame;

	frame /= 10;
	if ( (flags & FLIP_X_FLG) != 0 )
	{
		if ( mpoImagesFlx.size() == 0 )
			create_flipped_x();

		pImg = mpoImagesFlx[frame];
	}
	else
		pImg = mpoImages[frame];

	g.image_draw_trap( *pImg, x1a, x2a, ya, x1b, x2b, yb );
}

void sprite::draw( graphics &g, int frame, int x, int y, float scale, int flags )
{
	image	*pImg;
	bool	scaling;

	if ( mBaseScale != 1.0f )
		scale *= mBaseScale;

	if ( scale != 1.0f )
		scaling = true;
	else
		scaling = false;

	if ( frame < 0 )
		frame = -frame;

	frame /= 10;
	if ( (flags & FLIP_X_FLG) != 0 )
	{
		if ( mpoImagesFlx.size() == 0 )
			create_flipped_x();

		pImg = mpoImagesFlx[frame];
	}
	else
		pImg = mpoImages[frame];

	int w = (int)(pImg->mW * scale);
	int h = (int)(pImg->mH * scale);

	if ( mAlignX == RIGHT )
		x -= w;
	else
	if ( mAlignX == MIDDLE )
		x -= w/2;

	if ( mAlignY == BOTTOM )
		y -= h;
	else
	if ( mAlignY == MIDDLE )
		y -= h/2;

	if ( mHasShadow )
		draw_shadow( g, *pImg, x, y, w, h, scale );

	if ( (flags & 1) != 0 )
		g.image_draw_nt( *pImg, x, y );
	else
	{
		if ( (flags & TRANS100_100_FLG) != 0 )
			g.image_draw_100_100( *pImg, x, y, w, h );
		else
		{
			if ( scaling )
				g.image_draw( *pImg, x, y, w, h );
			else
				g.image_draw( *pImg, x, y );
		}
	}
}

void sprite::draw_shadow( graphics &g, image &img, int x, int y, int w, int h, float scale )
{
	int	yb;
	int	i, ii;
	int	off;
	int	sh_he;

	sh_he = (int)(14 * scale);

	yb = y + h;
	for (i=0; i < sh_he; ++i)
	{
		ii = i - sh_he/2;
		y = yb + ii;

		if ( ii > 0 )
			off = ii / 2 + 150 * (ii * ii) / sh_he / 100;
		else
			off = -ii / 2 + 100 * (ii * ii) / sh_he / 100;


		g.line_h_dk50( x+off, x+w-off, y );
	}
	/*
	int	xs, ws;

	sh_he = (int)(12 * scale);
	ws = w * 18 / 20;
	xs = x + (w-ws) / 2;
	if ( sh_he > 1 )
	{
	for (i=0; i < sh_he; ++i)
	{
	ii = i - (sh_he+1)/2;
	y = yb + ii - (sh_he+3)/4;

	if ( ii > 0 )
	off = ii / 2 + 150 * (ii * ii) / sh_he / 100;
	else
	off = -ii / 2 + 100 * (ii * ii) / sh_he / 100;

	g.line_h_dk25( xs+off, xs+ws-off, y );
	}
	}
	*/
}